import { Component } from '@angular/core';


declare var form: any;
declare var base: any;
declare var security: any;

@Component({
  templateUrl: './map.service.component.html'
})
export class MapServiceComponent {
  fs: any = null;
  datafs: any = null;
  ffs: any = null;
  data = new Date();

  serviceType: any = null;
  serviceStatus: any = null;
  servicePermission: any = null;
  coordinateSystem: any = null;
  dataServiceType: any = null;
  sourceMenu: any = null;
  mapTypeDic: any = null;
  stateDic: any = null;
  yearDic: any = null;
  defDic: any = null;
  gridDic: any = null;
  dataServeDic: any = null;
  dataSourceDic: any = null;
  serverNatureDic: any = null;

  publishParas = {};
  isPublish = false;

  constructor() {
    // 注册表单
    this.fs = new form.FormSimpleService('giscustomservice', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
    });

    this.datafs = new form.FormSimpleService('datadefinition', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
    });


    this.fs.loadData(true);

    // 文件服务
    this.ffs = new form.FileService();

    this.serviceType = new form.DictionaryService('d_service_type');
    this.serviceType.load('0');
    this.serviceStatus = new form.DictionaryService('d_service_status');
    this.serviceStatus.load('0');
    this.servicePermission = new form.DictionaryService('d_service_permission');
    this.servicePermission.load('0');
    this.coordinateSystem = new form.DictionaryService('d_coordinate_system');
    this.coordinateSystem.load('0');
    this.dataServiceType = new form.DictionaryService('d_data_service_type');
    this.dataServiceType.load('0');
    this.sourceMenu = new form.DictionaryService('p_gisresourcecontent');
    this.sourceMenu.loadAll();
    this.mapTypeDic = new form.DictionaryService('d_map_service_type');
    this.mapTypeDic.load('0');
    this.stateDic = new form.DictionaryService('d_service_status');
    this.stateDic.load('0');
    this.yearDic = new form.DictionaryService('dtfw_year');
    this.yearDic.load('0');
    // 数据定义
    this.defDic = new form.DictionaryService('g_data_definition');
    this.defDic.loadAll();

    // 栅格数据
    this.gridDic = new form.DictionaryService('g_grid_data');
    this.gridDic.loadAll();
    // 数据源服务器类型
    this.dataServeDic = new form.DictionaryService('d_souce_server_type');
    this.dataServeDic.loadAll();
    // 数据源类型字典
    this.dataSourceDic = new form.DictionaryService('d_data_source_type');
    // 数据源类型字典
    this.serverNatureDic = new form.DictionaryService('d_server_nature');
  }


  publish(item) {

    if (this.isPublish === true) {
      return;
    }

    this.isPublish = true;

    const paras = [];

    item.serviceSchemes.forEach(scheme => {
      this.datafs.defaultQueryObject = { id: scheme.source };

      this.datafs.loadData(true, (datas: any) => {
        const publishParas: any = {};

        if (scheme.extent && scheme.extent !== '') {
          const extent = JSON.parse(scheme.extent);
          publishParas.extent = [extent.xmin, extent.ymin, extent.xmax, extent.ymax];
        } else {
          publishParas.extent = null;
        }

        publishParas.minLevel = scheme.level;
        publishParas.maxLevel = scheme.level;
        publishParas.serviceName = item.cname;
        if (datas && datas[0]) {
          publishParas.source = datas[0].datatable;
        }
        paras.push(publishParas);

        if (paras.length === item.serviceSchemes.length) {
          setTimeout(() => {
            this.fs.loadData(true);
            this.isPublish = false;
          }, 3000);

          this.tmsServicePulish(item.id, paras);
        }
      });

    });

  }

  addService() {
    this.fs.er = { registertime: new Date(), registrantname: security.SecurityService.user.userName };
    this.fs.addRow(this.fs.er);
  }


  tmsServicePulish(id: string, where: any, success?: any, fail?: any) {
    const url: string = base.BaseService.getHostUrl() + '/' + this.fs.sn + '/publishtms/' + id;
    base.BaseService.post(url, where, success, fail);
  }

  change() {

  }


}
